package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImp extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public Page<SetmealDto> myPage(int page, int pageSize, String name) {

        //分页构造器
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);

        //拷贝好数据最后用于返回
        Page<SetmealDto> setmealDtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        //条件构造器中添加条件(模糊匹配)
        queryWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);

        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        //分页
        this.page(pageInfo, queryWrapper);

        //对象拷贝
        //除了records剩下的都拷贝
        BeanUtils.copyProperties(pageInfo, setmealDtoPage, "records");

        //将pageInfo中的records拿出来
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {

            SetmealDto setmealDto = new SetmealDto();
            //将records都赋值给setmealDto
            BeanUtils.copyProperties(item, setmealDto);

            Long categoryId = item.getCategoryId();

            Category category = categoryService.getById(categoryId);

            if (category != null) {

                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);

            }

            return setmealDto;

        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);

        return setmealDtoPage;
    }


    /*
     * 停售启售
     * */
    @Override
    public void myStatus(Integer status, List<Long> ids) {

        List<Setmeal> setmeals = setmealMapper.selectBatchIds(ids);

        setmeals.stream().forEach((item) -> {

            item.setStatus(status);
        });

        this.updateBatchById(setmeals);


    }


    /*
     * 新增套餐
     * */
    @Transactional//事务的注解要么全成功要么全失败
    public void mySave(SetmealDto setmealDto) {

        //添加套餐
        this.save(setmealDto);

        //在setmealDish中添加加入套餐的菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //里面的setmealId没有值要再次赋值
        setmealDishes.stream().map((item) -> {

            item.setSetmealId(setmealDto.getId());

            return item;

        }).collect(Collectors.toList());

        //修改完后再添加进去
        setmealDishService.saveBatch(setmealDishes);

    }


    /*
     * 批量删除
     * */
    @Override
    public void myDelete(List<Long> ids) {


        for (Long id : ids) {

            //如果套餐状态为停售则不能删除
            Setmeal setmeal = this.getById(id);

            Integer status = setmeal.getStatus();

            if (status == 1) {
                throw new CustomException("存在套裁状态为停售删除失败");
            } else {

                //先删除setmeal表中的套餐信息
                this.removeById(id);

                //再删除setmealDish中关联的菜品信息
                LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(SetmealDish::getSetmealId, id);
                setmealDishService.remove(queryWrapper);

            }


        }

    }

    /*
    * 修改套餐数据回显
    * */
    @Override
    public SetmealDto mygetById(Long id) {

        //先创建一个setmealDto类用于返回
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = this.getById(id);

        //对象拷贝
        BeanUtils.copyProperties(setmeal,setmealDto);

        //在往setmealDto中添加setmealDishes和categoryName属性
        Category category = categoryService.getById(setmeal.getCategoryId());
        String categoryName = category.getName();
        setmealDto.setCategoryName(categoryName);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> lists = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(lists);

        return setmealDto;
    }


    @Override
    public void myUpdate(SetmealDto setmealDto) {

        //setmeal表中要修改
        this.updateById(setmealDto);

        //setmealDish表中也要修改
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //把以前添加的菜品删去
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //设置修改后的setmealDish字段
        setmealDishes.stream().map((item) -> {

            item.setSetmealId(setmealDto.getId());

            return item;

        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);




    }

    /*
    * 用于在移动端展示套餐中的菜品
    * */

    @Override
    public List<SetmealDto> myList(Setmeal setmeal) {

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());

        queryWrapper.orderByAsc(Setmeal::getStatus).orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> setmeals = this.list(queryWrapper);

        //如果菜品为停售状态的时候则不显示在菜单中
        for (int i = 0; i < setmeals.size(); i++) {

            Setmeal setmeal1 = setmeals.get(i);

            Integer status = setmeal1.getStatus();
            if (status == 0){
                setmeals.remove(i);
            }
        }

        List<SetmealDto> setmealDtos = setmeals.stream().map((item) -> {

            SetmealDto setmealDto = new SetmealDto();

            BeanUtils.copyProperties(item, setmealDto);

            LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();

            lambdaQueryWrapper.eq(SetmealDish::getSetmealId, item.getId());

            List<SetmealDish> setmealDishes = setmealDishService.list(lambdaQueryWrapper);

            setmealDto.setSetmealDishes(setmealDishes);

            return setmealDto;

        }).collect(Collectors.toList());


        return setmealDtos;
    }
}
